package com.ruoyi.canteen.controller.v1;

import com.ruoyi.canteen.service.ICanteenCpService;
import com.ruoyi.canteen.vo.CartItemVo;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车模块
 * @Author Ryan
 * @Date 2021/10/1 15:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/v1/cart")
public class CartApiController {

    @Autowired
    private ICanteenCpService canteenCpService;

    /**
     * TODO 检测购物车项目：
     * 1- 必点 2-同一家门店
     * @param cartItemVo
     * @return
     */
    @PostMapping("/checkCart/")
    public AjaxResult checkCart(List<CartItemVo> cartItemVo){
        // 变量判断列表中是否存在必点类目下商品
        // 判断列表中餐品是否为同一家门店
        String cpIds = "";
        for(CartItemVo vo : cartItemVo){
            cpIds = cpIds + vo.getId() + ",";
        }
        if(cpIds.length() > 1){
            cpIds = cpIds.substring(0, cpIds.length() -1);
        }
        if(canteenCpService.isSameMd(cpIds)){
            return AjaxResult.success("同一门店");
        }else{
            return AjaxResult.error("请选择同一门店菜品下单");
        }
    }

}
